﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PortalCurriculo.Util.Controls.Wizard
{
    public sealed class Steps : WebControl
    {
        private System.Web.UI.WebControls.Wizard parentWizard;
        public string WizardToNavigate { get; set; }

        [DefaultValue("")]
        [CssClassProperty]
        public string ActiveStepCssClass { get; set; }

        [DefaultValue("")]
        [CssClassProperty]
        public string PreviousStepCssClass { get; set; }

        [DefaultValue("")]
        [CssClassProperty]
        public string NextStepCssClass { get; set; }

        [DefaultValue("")]
        [CssClassProperty]
        public string CellAfterActiveStepCssClass { get; set; }

        [DefaultValue("")]
        [CssClassProperty]
        public string CellAfterPreviousStepCssClass { get; set; }

        [DefaultValue("")]
        [CssClassProperty]
        public string CellAfterNextStepCssClass { get; set; }

        private System.Web.UI.WebControls.Wizard TargetWizard
        {
            get
            {
                if (!String.IsNullOrEmpty(WizardToNavigate))
                    parentWizard = Parent.FindControl(WizardToNavigate) as System.Web.UI.WebControls.Wizard;
                else
                    parentWizard = Parent.Parent.Parent.Parent as System.Web.UI.WebControls.Wizard;

                if (parentWizard != null) return parentWizard;

                throw new ArgumentOutOfRangeException(WizardToNavigate + " could not be found");
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            var table = new Table();
            var tableRow = new TableRow();

            int activeStepIndex = TargetWizard.ActiveStepIndex;
            int cont = 0;

            foreach (WizardStep step in TargetWizard.WizardSteps)
            {
                var tableCell = new TableCell();

                var label = new Label
                                {
                                    Text = step.Name,
                                    ID = step.ID,
                                };

                tableCell.CssClass = cont < activeStepIndex
                                         ? PreviousStepCssClass
                                         : (cont == activeStepIndex ? ActiveStepCssClass : NextStepCssClass);

                tableCell.Controls.Add(label);
                tableRow.Cells.Add(tableCell);


                tableCell = new TableCell
                                {
                                    CssClass = cont < activeStepIndex
                                                   ? CellAfterPreviousStepCssClass
                                                   : (cont == activeStepIndex
                                                          ? CellAfterActiveStepCssClass
                                                          : CellAfterNextStepCssClass)
                                };
                tableRow.Cells.Add(tableCell);

                cont++;
            }

            table.Rows.Add(tableRow);
            Controls.Add(table);
        }
    }
}